<template>
  <div class="menu-create">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ isEdit ? '编辑菜单' : '添加菜单' }}</span>
      </div>
      <el-form ref="elform" :model="form" label-width="80px">
        <el-form-item label="菜单名称" required>
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="菜单路径">
          <el-input v-model="form.href"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select v-model="form.parentId" placeholder="请选择上级菜单">
            <el-option :value="-1" label="无上级菜单"></el-option>
            <el-option
              v-for="item in parentMenuList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="前端图标">
          <el-input v-model="form.icon"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-radio-group v-model="form.shown">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="form.orderNum" :min="1" label="排序"></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button v-if="!isEdit">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { createOrUpdateMenu, getEditMenuInfo } from '/@/services/menu'
import { ElMessage } from 'element-plus'
import router from '/@/router/Index'
import { useRoute } from 'vue-router'

export default defineComponent({
  name: 'CreateOrEdit',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  setup() {
    const route = useRoute()
    let form = ref({
      parentId: -1, // 没有上级菜单
      name: '',
      href: '',
      icon: '',
      orderNum: 0,
      description: '',
      shown: false
    })
    const parentMenuList = ref([]) // 父级菜单列表
    const onSubmit = ref(async () => {
      // 1.表单验证
      // 2.提交表单
      const { data } = await createOrUpdateMenu(form.value)
      if (data.code === '000000') {
        ElMessage.success('提交成功')
        router.back()
      } else {
        ElMessage.error('操作失败')
      }
    })
    const loadMenuInfo = ref(async () => {
      const { data } = await getEditMenuInfo(route.params.id || -1)
      if (data.code === '000000') {
        if (data.data.menuInfo) {
          form.value = data.data.menuInfo
        }
        parentMenuList.value = data.data.parentMenuList
      }
    })
    return {
      form,
      parentMenuList,
      onSubmit,
      loadMenuInfo
    }
  },
  created() {
    this.loadMenuInfo()
  }
})
</script>

<style lang="scss" scoped></style>
